package de.op.projectc.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;

import de.op.projectc.MainGame;
import de.op.projectc.objects.Heli;
import de.op.projectc.objects.TerrainWorld;

public class GameScreen implements Screen {

	final MainGame game;
	private Stage stage;
	private Skin skin;
	
	Table table;
	Label labelScore;
	Label labelHighScore;
	LabelStyle style;
	Button button;
	
	Heli heli;
	TerrainWorld world;
	
	public GameScreen(final MainGame game) {
		this.game = game;

		createUI();

		Texture texture = new Texture(Gdx.files.internal("heli.png"));
		heli = new Heli(texture, Gdx.graphics.getWidth() / 2 - texture.getWidth() / 2, Gdx.graphics.getHeight() / 2 - texture.getHeight() / 2, 100, 100);
		
		world = new TerrainWorld(heli);
	}

	private void createUI() {
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);
		skin = new Skin(Gdx.files.internal("ui/uiskin.json"));

		table = new Table(skin);
		table.setFillParent(true);

		labelScore = new Label("Score: ", skin);
		labelHighScore = new Label("Highscore: ", skin);
		table.top();
		
		//ImageButton
		button = new TextButton("II", skin);
		button.addListener(new InputListener()
		{
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				//Dialog
				TextButton button2 = new TextButton("Spiel Beenden", skin);	  

				button2.addListener(new InputListener()
				{
					@Override
					public boolean touchDown(InputEvent event, float x, float y,
							int pointer, int button) {
						Gdx.app.exit();
						return true;
					}
				});
				Dialog dialog = new Dialog("PAUSE", skin);
				dialog.getContentTable().defaults().width(Gdx.graphics.getWidth() / 2).pad(3);
				dialog.getButtonTable().defaults().width(Gdx.graphics.getWidth() / 2).pad(3);
				dialog.button("Zurueck zum Spiel");
				dialog.getButtonTable().row();
				dialog.button(button2);
				dialog.show(stage);
				
				return true;
			}
		});

		labelScore.setFontScale(2);
		labelHighScore.setFontScale(2);

		table.defaults().pad(5);
		table.add(labelScore).left().expandX();
		table.add(labelHighScore).left().expandX();
		table.add(button).size(labelScore.getPrefHeight());

		table.debug();

		stage.addActor(table);
	
		}
	
	@Override
	public void render(float delta) {
		this.labelScore.setText("Score: " + Gdx.graphics.getFramesPerSecond());
		//Gdx.gl.glClearColor(0.5f, 0.3f, 0.3f, 1);
		//Gdx.gl.glClearColor(1, 0, 1, 0);
        //Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		Gdx.graphics.getGL20().glClearColor(1f, 1f, 1f, 1);
		Gdx.graphics.getGL20().glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		//kp testen
		//Gdx.gl20.glEnable(GL20.GL_BLEND);
		//Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
		
		world.update(delta);
        world.render(game.batch);
        
        heli.update(delta);
        
        game.batch.begin();
        heli.render(game.batch);
        game.batch.end();
        
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

        //Table.drawDebug(stage);
	}
	
	@Override
	public void dispose() {
		this.world.dispose();
	}

	@Override
	public void hide() {
		
	}

	@Override
	public void pause() {
		
	}


	@Override
	public void resize(int width, int height) {
		stage.setViewport(width, height, false);
	}

	@Override
	public void resume() {
		
	}

	@Override
	public void show() {
		
	}

}
